﻿using Furion.DynamicApiController;
using Newtonsoft.Json;
using StackExchange.Redis;
using WesWeb.InOrder.Dto;
using WesWebCore;

namespace WesWeb.InOrder;

public class InOrderService : IDynamicApiController
{
    private readonly IConnectionMultiplexer _redisConnection;

    public InOrderService(IConnectionMultiplexer redisConnection)
    {
        _redisConnection = redisConnection;
    }

    public async Task AddOrder(AddInOrderDto input)
    {
        var db = _redisConnection.GetDatabase();
        var key = input.ContCode + input.LabelNo;
        input.Id = Guid.NewGuid().ToString("N");
        var json = JsonConvert.SerializeObject(input);
        await db.HashSetAsync(RedisKeys.InOrder, key, json);
    }

    public async Task<List<AddInOrderDto>> GetAll()
    {
        var db = _redisConnection.GetDatabase();
        var entries = await db.HashGetAllAsync(RedisKeys.InOrder);
        return entries
        .Select(x => JsonConvert.DeserializeObject<AddInOrderDto>(x.Value.ToString()))
        .ToList();
    }
}